package edu.zjut.hjg.saas.biz.service.impl;

import edu.zjut.hjg.saas.biz.dao.RecipeDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.CookBook;
import edu.zjut.hjg.saas.biz.model.Recipe;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.RecipeService;
import edu.zjut.hjg.saas.biz.support.OperateType;

public class RecipeServiceImpl implements RecipeService {

    private RecipeDao  recipeDao;
    private UserLogDao userLogDao;

    public void setRecipeDao(RecipeDao recipeDao) {
        this.recipeDao = recipeDao;
    }

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    @Override
    public Recipe findByAppId(int appId) {
        return recipeDao.findByAppId(appId);
    }

    @Override
    public void edit(int appId, int userId, CookBook cookBook, int theWeek, String operator) {
        Recipe recipe = recipeDao.findByAppId(appId);
        boolean update = true;
        if (recipe == null) {
            recipe = new Recipe();
            update = false;
        }
        recipe.setCookBook(cookBook);
        recipe.getKindergarten().setId(appId);
        recipe.getUser().setId(userId);
        recipe.setTheWeek(theWeek);

        UserLog userLog = new UserLog();
        userLog.setKindergarten(recipe.getKindergarten());
        userLog.setLoginId(operator);
        if (update) {
            recipeDao.update(recipe);
            userLog.setOperateType(OperateType.UPDATE);
            userLog.setDescription("修改了本周食谱");
        } else {
            recipeDao.create(recipe);
            userLog.setOperateType(OperateType.INSERT);
            userLog.setDescription("添加了本周食谱");
        }
        userLogDao.create(userLog);
    }
}
